package com.weiyu.bultrasound.core.task;

import com.weiyu.bultrasound.core.model.entity.Applications;
import com.weiyu.bultrasound.core.service.ApplicationsService;
import com.weiyu.bultrasound.core.service.FilesUploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 *@description 同步任务，用于单次申请的失败补偿
 *@author abner chow
 *@date 2019-08-23 14:25
 */
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class SyncTask {

    private static final Logger logger = LoggerFactory.getLogger(SyncTask.class);

    @Autowired
    ApplicationsService applicationsService;

    @Autowired
    FilesUploadService filesUploadService;

    @Scheduled(cron = "0 0/2 * * * ?")//每2分钟执行一次
    //或直接指定时间间隔，例如：5秒
    //@Scheduled(fixedRate=5000)
    private void configureTasks() {
        logger.info("执行同步定时任务 " + LocalDateTime.now());
        //查找已上传的申请列表
        List<Applications> list = applicationsService.getUnsynList();
        if (!CollectionUtils.isEmpty(list)) {
            logger.info("此次定时任务需同步的列表" + list.toString());
            for (Applications applications : list) {
                logger.info("申请单id：" + applications.getId() + " 开始同步");
                Boolean result =  filesUploadService.synFilesToFtp(applications.getId());
                logger.info("申请单id：" + applications.getId() + " 同步结束，执行结果：" + result);
            }
        } else {
            logger.info("本次无需同步");
        }
    }

}
